/**
 * NearShop ChangeCityActivity.java com.android.nearshop.app.activity.setting
 * 
 */
package com.android.nearshop.app.activity.setting;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.android.nearshop.R;
import com.android.nearshop.app.MyApplication;
import com.android.nearshop.app.activity.common.LoadableActivity;
import com.android.nearshop.app.adapter.listadapter.Text3ListAdapter;
import com.android.nearshop.app.adapter.listadapter.Text3ListAdapter.Text3Item;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.business.cache.DataCacheManager;
import com.android.nearshop.io.protocol.out.Area;
import com.android.nearshop.util.NotifyListener;
import com.android.nearshop.util.Util;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-18
 * @see : 改变镇区
 * @Copyright : copyrights reserved by personal 2007-2012
 **********************************************************/
public class ChangeCityActivity extends LoadableActivity 
implements OnItemClickListener
{
    public static final int CODE_REQUEST = 0x8810;
	 
    private TextView location;
    private Text3ListAdapter listAdapter;
    private boolean addAll;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_change_city);
        
        location = (TextView) findViewById(R.id.location);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setDivider(getResources().getDrawable(R.drawable.content_div));
        listAdapter = new Text3ListAdapter(this);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(this);      
        location.setText(Util.getCityText(this));     
        loadData();
    }

    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
    	titleBar.setLeftImage(0);
        titleBar.setRightVisible(false);
        titleBar.setTitle(R.string.tx_setting);
    }

    private void loadData()
    {
        mLoadingView.showLoadingView();
        DataCacheManager.getInstance().getAreaList(
                new NotifyListener<List<Area>>()
                {
                    @Override
                    public void onSucceed(List<Area> result)
                    {
                        if (result != null)
                        {
                            int size = result.size();
                            List<Text3Item> list = new ArrayList<Text3Item>(size);
                            //alter by huangyue 避免出现全部
                            Area one = null;
                            
                            for (int i = 0; i < size; i++)
                            {
                            	one = result.get(i).setOpenVisible(false);
//                            	if(one.areaId == 0){
//                            		continue;
//                            	}
                                list.add(one);
                            }
                            //end
                            
                            
                            listAdapter.setList(list);
                            mLoadingView.showMainView();
                        } else
                        {
                            mLoadingView.showLoadingErrView(R.string.no_result,false);
                        }
                    }

                    @Override
                    public void onError(int resultCode)
                    {
                        mLoadingView.showLoadingErrView(false);
                    }
                });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id)
    {
        Area item = (Area) listAdapter.getItem(position);
        MyApplication.getContext().setLocCityMode(item.areaId);
        location.setText(item.areaName);       
    }

    @Override
    public void refreshView()
    {
        loadData();
    }
    
    public static void startActivity(Activity context)
    {
        context.startActivity(getStartActivity(context));
    }

    public static Intent getStartActivity(Activity context)
    {
        Intent intent = new Intent(context, ChangeCityActivity.class);
        return intent;
    }

}
